# -*- coding: utf-8 -*-

import os
import py_compile
import shutil
import zipfile

if os.path.isfile( 'SIL.zip' ): os.remove( 'SIL.zip' )

release_directory = os.path.join( 'release', 'SIL' )

shutil.copytree( '.', release_directory )



def cleanse( path ):
    for node in os.listdir( path ):
        if path[-7:] == 'release':
            return
        if node == 'geany_run_script.sh' or node[-4:] in ( '.pyc', '.pyo' ):
            os.remove( os.path.join( path, node ) )
        elif node[-3:] == '.py' and node != 'SIL.py':
            py_compile.compile(
                os.path.join( path, node ),
                os.path.join( release_directory, path, node + 'c' )
            )
            os.remove( os.path.join( release_directory, path, node ) )
        elif os.path.isdir( os.path.join( path, node ) ):
            cleanse( os.path.join( path, node ) )

cleanse( '.' )

#os.system( '~/Desktop/pyinstaller-1.4/Configure.py' )
#os.system( '~/Desktop/pyinstaller-1.4/Makespec.py --onefile SIL.py' )
#os.system( '~/Desktop/pyinstaller-1.4/Build.py SIL.spec' )
#shutil.move( 'dist/SIL', 'release/SIL' )

os.system( 'wine c:\\\python25\\\python ~/Desktop/pyinstaller-1.4/Configure.py' )
os.system( 'wine c:\\\python25\\\python ~/Desktop/pyinstaller-1.4/Makespec.py --onefile SIL.py' )
os.system( 'wine c:\\\python25\\\python ~/Desktop/pyinstaller-1.4/Build.py SIL.spec' )
shutil.move( 'dist/SIL.exe', 'release/SIL' )

os.remove( os.path.join( release_directory, 'build.pyc' ) )
os.remove( os.path.join( release_directory, 'builder.pyc' ) )
os.remove( os.path.join( release_directory, 'profiling.pyc' ) )


os.chdir( 'release' )
os.system( 'zip -r SIL.zip SIL' )
shutil.move( 'SIL.zip', '..' )
os.chdir( '..' )
shutil.rmtree( 'release' )
shutil.rmtree( 'build' )
shutil.rmtree( 'dist' )
os.remove( 'SIL.spec' )   
os.remove( 'warnSIL.txt' )
for node in os.listdir( '.' ):
    if node[-4:] == '.log': os.remove( node )
